package com.bytedance.improvingroad.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.bytedance.improvingroad.utils.Utils

/**
 * Created by chenxiong
 * date 12/22/21
 */
class MyView @JvmOverloads constructor(context: Context, attr: AttributeSet? = null, style: Int = 0): View(
    context,attr,style
) {
    private val text1 = "世事如书，我偏爱你这一句，愿做个逗号"
    private val text2 = "，待在你脚边。但你有自己的朗读者，而"
    private val text3 = "我只是个摆渡人。"
    private val mPaint = Paint().apply {
        textSize = 50f
        color = Color.RED
    }
    init {
//        initListener()
    }
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val rect = Rect()
        mPaint.getTextBounds(text1,0,text1.length,rect)
        canvas?.drawText(text1,0f,rect.height().toFloat(),mPaint)
        canvas?.drawText(text2,0f,rect.height()*2+Utils.dp2px(2).toFloat(),mPaint)
        canvas?.drawText(text3,0f,rect.height()*3+Utils.dp2px(2).toFloat()*2,mPaint)
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initListener() {
        setOnClickListener {
            Log.e("cx","OnClickListener")
        }
        setOnTouchListener { v, event ->
            Log.e("cx","OnTouchListener")
            false
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.e("cx","onTouchEvent")
        //return true就不会调用onClick
        return super.onTouchEvent(event)
    }
}